/**
 * www.mbaobao.com Inc.
 * Copyright (c) 2013 All Rights Reserved.
 */
package top.rushpeak.edu03.core.mq;

import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import com.google.gson.Gson;

/**
 *                       
 * @Filename MessageSender.java
 *
 * @Description 
 *
 * @Version 1.0
 *
 * @Author fred
 *
 * @Email kuci@mbaobao.com
 *       
 * @History
 *<li>Author: fred</li>
 *<li>Date: 2014-6-30</li>
 *<li>Version: 1.0</li>
 *<li>Content: create</li>
 *
 */
public class MessageSender {
	
	private static final Logger	LOG		= Logger.getLogger(MessageSender.class);
	
	private static final Gson	gson	= new Gson();
	
	@Autowired
	private JmsTemplate			jmsQueueTemplate;
	
	@Autowired
	private JmsTemplate			jmsTopicTemplate;
	
	public void sendQueueMessage(final EventEnum action, final Map<String, String> data) {
		LOG.info(String.format("sendQueueMessage,action=%s,data=%s", action, data));
		try {
			jmsQueueTemplate.send(new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					TextMessage message = session.createTextMessage();
					message.setStringProperty("type", action.code());
					message.setText(gson.toJson(data));
					return message;
				}
			});
		} catch (Exception e) {
			LOG.error("JMS发生未知异常", e);
		}
	}
	
	public void sendQueueMessageTopic(final EventEnum action, final Map<String, String> data) {
		LOG.info(String.format("sendQueueMessage,action=%s,data=%s", action, data));
		try {
			jmsTopicTemplate.send(new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					TextMessage message = session.createTextMessage();
					message.setStringProperty("type", action.code());
					message.setText(gson.toJson(data));
					return message;
				}
			});
		} catch (Exception e) {
			LOG.error("JMS发生未知异常", e);
		}
	}
	
	public <T> void sendQueueMessage(final EventEnum action, final MessageContent<T> content) {
		LOG.info(String.format("sendQueueMessage,action=%s,content=%s", action, content));
		try {
			jmsQueueTemplate.send(new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					ObjectMessage objMsg = session.createObjectMessage();
					objMsg.setObject(content);
					objMsg.setStringProperty("type", action.code());
					return objMsg;
				}
			});
		} catch (Exception e) {
			LOG.error("JMS发生未知异常", e);
		}
	}
	
	public <T> void sendQueueMessageTopic(final EventEnum action, final MessageContent<T> content) {
		LOG.info(String.format("sendQueueMessage,action=%s,content=%s", action, content));
		try {
			jmsTopicTemplate.send(new MessageCreator() {
				@Override
				public Message createMessage(Session session) throws JMSException {
					ObjectMessage objMsg = session.createObjectMessage();
					objMsg.setObject(content);
					objMsg.setStringProperty("type", action.code());
					return objMsg;
				}
			});
		} catch (Exception e) {
			LOG.error("JMS发生未知异常", e);
		}
	}
	
}
